//
//  logInViewController.m
//  ziLu_ios
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "logInViewController.h"

@interface logInViewController ()
{
    NSInteger repeatCount;
    NSTimer *timer;
}
@end

@implementation logInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1];
    repeatCount =60;
    //按钮框
    CALayer *layer = _yanZheng_btn.layer;
    layer.borderColor = [UIColor colorWithRed:248/255.0 green:141/255.0 blue:27/255.0 alpha:1].CGColor;
    layer.borderWidth = (2.5 / [UIScreen mainScreen].scale );
    _yanZheng_btn.layer.masksToBounds = YES;
    _yanZheng_btn.layer.cornerRadius =5;
    
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 发送验证码倒计时

- (void)veriCountDown:(NSTimer *)sender {
    
    repeatCount--;
    
    NSString *timerText = [NSString stringWithFormat:@"%ld秒",(long)repeatCount];
    
    //self.verifyCodeLabel.textColor = [UIColor grayColor];
     [_yanZheng_btn setTitle:timerText forState:UIControlStateNormal];
    
    // 如果倒计时到了0
    if (repeatCount == 0) {
        repeatCount = 60;
        [timer invalidate];
        timer = nil;
        
//        self.verifyCodeLabel.textColor = [Utils colorWithRGB:THEME_RED alpha:1];
//        self.verifyCodeLabel.text = @"获取验证码";
//        self.verifyCodeLabel.userInteractionEnabled = YES;
        
        // 按钮重新可以被点击
                self.yanZheng_btn.enabled = YES;
                [_yanZheng_btn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
                _yanZheng_btn.titleLabel.text = @"获取验证码";
        CALayer *layer = _yanZheng_btn.layer;
        layer.borderColor = [UIColor colorWithRed:247/255.0 green:119/255.0 blue:0/255.0 alpha:1].CGColor;
        //背景颜色
        _yanZheng_btn.backgroundColor =[UIColor colorWithRed:255/255.0 green:249/255.0 blue:240/255.0 alpha:1];
        //文字颜色
        [_yanZheng_btn setTitleColor:[UIColor colorWithRed:248/255.0 green:131/255.0 blue:0/255.0 alpha:1] forState:UIControlStateNormal];
        
    }
    
}
#pragma mark - 验证手机号码
- (BOOL)checkTel:(NSString *)str

{
    
    if ([str length] == 0) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号为空" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        
        [alert show];
        
        return NO;
        
    }
    
    //1[0-9]{10}
    
    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
    
    //    NSString *regex = @"[0-9]{11}";
    
    //    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    NSString * regex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        
        [alert show];
        
        
        return NO;
        
    }
    
    
    return YES;
    
}
#pragma  mark 验证码
- (IBAction)yanZheng:(id)sender {
    //验证手机号
    BOOL isMatch = [self checkTel:self.shouJi.text];
    if (!isMatch) {
        return;
    }
    
        NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:_shouJi.text,@"mobile",nil];
    [afn_api MyApi:dictionary url:AFN_Address(@"/user/sendMsg") success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"status"] isEqualToString:@"ok"]) {
            NSLog(@"吃了吗");
            self.yanZheng_btn.enabled = NO;
           //按钮不可点击状态下颜色
//            框的颜色
            CALayer *layer = _yanZheng_btn.layer;
            layer.borderColor = [UIColor colorWithRed:175/255.0 green:175/255.0 blue:175/255.0 alpha:1].CGColor;
            //背景颜色
            _yanZheng_btn.backgroundColor =[UIColor colorWithRed:251/255.0 green:251/255.0 blue:251/255.0 alpha:1];
            //文字颜色
            [_yanZheng_btn setTitleColor:[UIColor colorWithRed:174/255.0 green:174/255.0 blue:174/255.0 alpha:1] forState:UIControlStateNormal];
            
            timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(veriCountDown:) userInfo:nil repeats:YES];
            
            
            // 不加这句不运行
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error );
    }];
}

#pragma mark - 收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.shouJi resignFirstResponder];
    [self.miMa resignFirstResponder];
    [self.yanZheng resignFirstResponder];
}
- (IBAction)zhuCe:(id)sender {
    if ([_yanZheng.text isEqualToString:@""] && [_miMa.text isEqualToString:@""]  && [_yanZheng.text isEqualToString:@""] ) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请完善信息再申请" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        
        [alert show];
    }else{

        NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:_shouJi.text,@"mobile",_miMa.text,@"password",@"1",@"channel",_yanZheng.text,@"vCode",nil];
        
        [afn_api MyApi:dictionary url:AFN_Address(@"/user/register") success:^(id responseObject) {
            NSLog(@"注册：%@",[responseObject objectForKey:@"message"] );
            
        } failure:^(NSError *error) {
            
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
